<?php

class Resend extends CI_Controller
{
	/**
	 * Constructor that makes sure no one loged in can access the resend pages (if you are loged in you don't need the resend pages).
	 *
	 * @param void
	 * @return void
	 */
	function __construct()
	{
		parent::__construct();
		
		//Determine if the user is loged in.
		if ($this->session->userdata('user_logedin')) {
			//If the user is loged in then redirect to start page.
			redirect('start');
		}
		else { //Running code that is needed at the beginning of all functions.
			//Loading libraries for form handling.
			$this->load->helper('form');
			$this->load->library('form_validation');
			
			//Loading basic templates.
			$this->load->view('templates/v_header');
			$this->load->view('templates/v_menu_basic');
			
			//Setting the delimiters for error messages so they can be styled by CSS.
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
		}
	}
	
	/**
	 * Default function that redirects the user to the login page in case no specific function is called.
	 *
	 * @param void
	 * @return void
	 */
	function index()
	{
		redirect('logon');
	}
	
	/**
	 * Function for sending a link for resetting the password of an user account.
	 *
	 * @param void
	 * @return void
	 */
	function password()
	{
		//Setting the form validation rules.
		$this->form_validation->set_rules('username', 'username', 'required');
		
		//Store the result of the resending.
		$resend_successful = FALSE;
		
		//Resetting resend error message.
		$page_data['resend_error'] = '';
		
		//Validating data input.
		if ($this->form_validation->run())
		{
			//Creating an user object.
			$this->load->model('m_user', 'user');
			
			//Getting the entered username.
			$username = $this->input->post('username');
			
			//Trying to resend activation link.
			$result = $this->user->resend_password($username);
			
			//Checking if the activation link was resent.
			if ($result == 0) {
				//Load success page.
				$this->load->view('pages/v_resend_password_success');
				
				//Set the rensending to success.
				$resend_successful = TRUE;
			}
			else {
				//Setting an error message saying that the activation link was not sent.
				$page_data['resend_error'] = '<p class="error">It was not possible to send a mail with a reset-password-link.</p>';
			}
		}
		
		//Load the form again if the resending was unsuccessful.
		if (!$resend_successful) {
			//Repopulating the form.
			$page_data['entered_username'] = set_value('username');
			
			//Load page.
			$this->load->view('pages/v_resend_password', $page_data);
		}
		
		//Load footer.
		$this->load->view('templates/v_footer');
	}
	
	/**
	 * Function for resending the username for a user account.
	 *
	 * @param void
	 * @return void
	 */
	function username()
	{
		//Setting the form validation rules.
		$this->form_validation->set_rules('email', 'email', 'required');
		
		//Store the result of the resending.
		$resend_successful = FALSE;
		
		//Resetting resend error message.
		$page_data['resend_error'] = '';
		
		//Validating data input.
		if ($this->form_validation->run())
		{
			//Creating an user object.
			$this->load->model('m_user', 'user');
			
			//Getting the entered e-mail.
			$email = $this->input->post('email');
			
			//Trying to resend the username.
			$result = $this->user->resend_username($email);
			
			//Checking if the activation link was resent.
			if ($result == 0) {
				//Load success page.
				$this->load->view('pages/v_resend_username_success');
				
				//Set the rensending to success.
				$resend_successful = TRUE;
			}
			else {
				//Setting an error message saying that the activation link was not sent.
				$page_data['resend_error'] = '<p class="error">It was not possible to resend any username.</p>';
			}
		}
		
		//Load the form again if the resending was unsuccessful.
		if (!$resend_successful) {
			//Repopulating the form.
			$page_data['entered_email'] = set_value('email');
			
			//Load page.
			$this->load->view('pages/v_resend_username', $page_data);
		}
		
		//Load footer.
		$this->load->view('templates/v_footer');
	}
	
	/**
	 * Function for resending the activation link for a user account.
	 *
	 * @param void
	 * @return void
	 */
	function activation()
	{
		//Setting the form validation rules.
		$this->form_validation->set_rules('username', 'username', 'required');
		
		//Store the result of the resending.
		$resend_successful = FALSE;
		
		//Resetting resend error message.
		$page_data['resend_error'] = '';
		
		//Validating data input.
		if ($this->form_validation->run())
		{
			//Creating an user object.
			$this->load->model('m_user', 'user');
			
			//Getting the entered username.
			$username = $this->input->post('username');
			
			//Trying to resend activation link.
			$result = $this->user->resend_activation($username);
			
			//Checking if the activation link was resent.
			if ($result == 0) {
				//Load success page.
				$this->load->view('pages/v_resend_activation_success');
				
				//Set the rensending to success.
				$resend_successful = TRUE;
			}
			else {
				//Setting an error message saying that the activation link was not sent.
				$page_data['resend_error'] = '<p class="error">It was not possible to resend any activation link.</p>';
			}
		}
		
		//Load the form again if the resending was unsuccessful.
		if (!$resend_successful) {
			//Repopulating the form.
			$page_data['entered_username'] = set_value('username');
			
			//Load page.
			$this->load->view('pages/v_resend_activation', $page_data);
		}
		
		//Load footer.
		$this->load->view('templates/v_footer');
	}
}
?>